<?php

namespace App\Http\Controllers;

use App\Models\Article;
use App\Models\Like;
use App\Models\SelectedGoodsLog;
use App\Util\Url;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\BaseController;

class ArticleController extends BaseController
{

    public function getArticle(Request $request)
    {
        $res = $request->input();
        $res['work_id'] = 1;
        $res['id'] = 1;
        $res['user_id'] = 2;
        $res['collect_type'] = 1;
        $resource = (new Url())::getHttpHost();
        return view('article',['id' => $res['id'],'user_id' => $res['user_id'],'collect_type' => $res['collect_type'],'work_id' => $res['work_id'],'resource'=>$resource]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $param = $request->input() ?: [];
        $course = (new Article())->getList($param);
        return  $this->ResultJson(1,'获取成功!',$course);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     * 点赞
     */
    public function create(Request $request)
    {
        $param = $request->input();
        if (empty($param['article_id'])) {
            return $this->ResultJson(0, '文章ID不能为空!', []);
        }
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, '用户ID不能为空!', []);
        }
        if (empty($param['type'])) {
//            return $this->ResultJson(0, '类型不能为空!', []);
            $param['type'] = 1;
        }
        $course = (new Like())->setWorkFollow($param);
        switch ($course) {
            case 1:
                (new Article())->where([['id','=',$param['article_id']]])->decrement('likes',1);
                return $this->ResultJson(1, '取消点赞成功!', []);
                break;
            case 2:
                return $this->ResultJson(0, '取消点赞失败!', []);
                break;
            case 3:
                (new Article())->where([['id','=',$param['article_id']]])->increment('likes',1);
                return $this->ResultJson(1, '点赞成功!', []);
                break;
            case 4:
                return $this->ResultJson(0, '点赞失败!', []);
                break;
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     * 用户端查看详情
     */
    public function show(Request $request)
    {
        $param = $request->input() ?: [];
        if(empty($param['article_id'])){
            return  $this->ResultJson(0,'文章id不能为空!',[]);
        } if(empty($param['work_id'])){
            return  $this->ResultJson(0,'工作ID不能为空!',[]);
        }
        $course = (new Article())->getArticleInfo($param);
        return  $this->ResultJson(1,'获取成功!',$course);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     * 医护室查看文章详情
     */
    public function detail(Request $request)
    {
        $param = $request->input();
        if(empty($param['id'])){
            return  $this->ResultJson(0,'文章ID不能为空!',[]);
        }
        $course = (new Article())->getArticleInfoForDoctory($param);
        return  $this->ResultJson(1,'获取成功!',$course);
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     * 提交选中文章
     */
    public function addRelateArticles(Request $request)
    {
        $param = $request->input();
        if(empty($param['work_id'])){
            return  $this->ResultJson(0,'工作ID不能为空!',[]);
        }
        if (empty($param['cate_id'])) {
            return $this->ResultJson(0, '分类ID不能为空!', []);
        }
        if (empty($param['article_ids'])) {
            return $this->ResultJson(0, '文章ID不能为空!', []);
        }
        $course = (new Article())->addRelateArticles($param);
        if($course === true){
            return  $this->ResultJson(1,'添加成功!',[]);
        }
        return  $this->ResultJson(0,'添加失败!',[]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     * 添加选中
     */
    public function addSelectedArticle(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, '工作室ID不能为空!', []);
        }
        if (empty($param['goods_id'])) {
            return $this->ResultJson(0, '商品ID不能为空!', []);
        }
        if (empty($param['type'])) {
            return $this->ResultJson(0, '类型不能为空!', []);
        }
        $res = (new SelectedGoodsLog())->addSelectedGoodsLog($param);
        if ($res) {
            return $this->ResultJson(1, '添加成功!', []);
        }
        return $this->ResultJson(0, '添加失败!', []);
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     * 删除选中
     */
    public function delSelectedArticle(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, '工作室ID不能为空!', []);
        }
        if (empty($param['goods_id'])) {
            return $this->ResultJson(0, '商品ID不能为空!', []);
        }
        if (empty($param['type'])) {
            return $this->ResultJson(0, '类型不能为空!', []);
        }
        $res = (new SelectedGoodsLog())->delSelectedGoodsLog($param);
        if ($res) {
            return $this->ResultJson(1, '删除成功!', []);
        }
        return $this->ResultJson(0, '删除失败!', []);
    }





    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     * 已选文章列表
     */
    public function getSelectedArticle(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, '工作室ID不能为空!', []);
        }
        if (empty($param['type'])) {
            return $this->ResultJson(0, '类型不能为空!', []);
        }
        $res = (new SelectedGoodsLog())->getSelectedGoodsLog($param);
        if ($res) {
            return $this->ResultJson(1, '返回成功!', $res);
        }
        return $this->ResultJson(1, '暂无数据!', []);
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     * 公益库
     */
    public function getAllArticleList(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, '工作室ID不能为空!', []);
        }
        $res = (new Article())->getAllArticleList($param);
        if ($res) {
            return $this->ResultJson(1, '获取成功!', $res);
        }
        return $this->ResultJson(1, '暂无数据', []);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     * 科普文章发布
     */
    public function addArticle(Request $request)
    {
        $param = $request->input();
        if (!isset($param['id'])) {
            if (empty($param['doctor_id'])) {
                return $this->ResultJson(0, '医师ID不能为空!', []);
            }
            if (empty($param['work_id'])) {
                return $this->ResultJson(0, '工作室ID不能为空!', []);
            }
            if (empty($param['article_title'])) {
                return $this->ResultJson(0, '主题不能为空!', []);
            }
            if (empty($param['clinic_cate_id'])) {
                return $this->ResultJson(0, '科室ID不能为空!', []);
            }
            if (empty($param['self_cate_id'])) {
                return $this->ResultJson(0, '本科室分类ID不能为空!', []);
            }
//            if (empty($param['art_types'])) {
//                return $this->ResultJson(0, '类型不能为空!', []);
//            }
            if (empty($param['content'])) {
                return $this->ResultJson(0, '内容不能为空!', []);
            }
        }
        $res = (new Article())->addArticle($param);
        if ($res === true) {
            if(isset($param['id'])){
                return $this->ResultJson(1, '编辑成功!', []);
            }
            return $this->ResultJson(1, '添加成功!', []);
        }
        return $this->ResultJson(0, '添加失败!', []);
    }

    /**
     * 医护端我的文章
     */
    public function myArticles(Request $request)
    {
        $param = $request->input();
//        if (empty($param['doctor_id'])) {
//            return $this->ResultJson(0, '医师ID不能为空!', []);
//        }
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, '工作室ID不能为空!', []);
        }
        $res = (new Article())->getMyArticles($param);
        if ($res) {
            return $this->ResultJson(1, '获取成功!', $res);
        }
        return $this->ResultJson(1, '暂无数据!', []);
    }


    /**
     * 删除我的文章
     */
    public function delMyArticles(Request $request)
    {
        $param = $request->input();
        if (empty($param['doctor_id'])) {
            return $this->ResultJson(0, '医师ID不能为空!', []);
        }
        if (empty($param['article_id'])) {
            return $this->ResultJson(0, '文章ID不能为空!', []);
        }
        $res = (new Article())->delMyArticles($param);
        if ($res===true) {
            return $this->ResultJson(1, '删除成功!', []);
        }
        return $this->ResultJson(0, $res, []);
    }


}
